//
//  IndexView.swift
//  LuYao
//
//  Created by vase on 2022/3/1.
//

import UIKit

class IndexView: UIView {
    /** 绑定的VC*/
    var bind : IndexViewController?
    /** 安全区域*/
    var safeView : UIView = UIView.init()
    /** 标识主人的Label*/
    var introduceLabel : UILabel = UILabel.init()
    //设备数量的Label
    var devicesLabel : UILabel = UILabel.init()
    /** 展示信息的列表*/
    var infoTableView : UITableView = UITableView.init()
    //信息集合
    var indexInfos : [IndexInfoModel] = []

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.white
        let backView = UIImageView.init(frame: CGRect.init(x: 0, y: 0, width: screenWidth, height: screenHeight))
        backView.image = UIImage.init(named: "./home.jpg")
        self.addSubview(backView)
        /** 安全区域*/
//        safeView.frame = CGRect.init(x: 0, y: stateRegionHeight, width: screenWidth, height: screenHeight-stateRegionHeight-bottomRegionHeight)
//        safeView.backgroundColor = UIColor.gray
//        self.addSubview(safeView)
        //初始化数据
        initData()
        //渲染页面信息
        introduceLabel.text = "玲娜贝儿的家"
        introduceLabel.textColor = UIColor.white
        introduceLabel.font = UIFont.boldSystemFont(ofSize: 25)
        introduceLabel.sizeToFit()
        introduceLabel.frame.origin = CGPoint.init(x: 30, y: stateRegionHeight+80)
        self.addSubview(introduceLabel)
        devicesLabel.text = "当前共计5个设备"
        devicesLabel.textColor = UIColor.white
        devicesLabel.font = UIFont.systemFont(ofSize: 15)
        devicesLabel.sizeToFit()
        devicesLabel.frame.origin = CGPoint.init(x: 30, y: introduceLabel.frame.maxY+10)
        self.addSubview(devicesLabel)
        /** 信息列表*/
        infoTableView.frame = CGRect.init(x: 15, y: devicesLabel.frame.maxY+80, width: screenWidth-30, height: screenHeight-devicesLabel.frame.maxY-80-bottomRegionHeight)
        infoTableView.backgroundColor = UIColor.clear
        infoTableView.delegate = self
        infoTableView.dataSource = self
        infoTableView.showsVerticalScrollIndicator = false
        infoTableView.tableFooterView = UIView.init(frame: CGRect.zero)
        self.addSubview(infoTableView)
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    /** 自定义的便捷初始化函数*/
    convenience init(frame: CGRect, bindVC : IndexViewController) {
        self.init(frame: frame)
        bind = bindVC
    }
    
    //初始化数据
    func initData() {
        indexInfos.removeAll()
        //温度湿度
        let model1 = IndexInfoModel.init()
        model1.location = "家庭卧室"
        model1.optionNames = ["temperature", "humidity"]
        model1.optionInfos["temperature"] = "温度：27.5˚C"
        model1.optionInfos["humidity"] = "湿度：56%"
        indexInfos.append(model1)
        //室内亮度、气体浓度等
        let model2 = IndexInfoModel.init()
        model2.location = "家庭厨房"
        model2.optionNames = ["brightness", "gas"]
        model2.optionInfos["brightness"] = "光照强度：66%"
        model2.optionInfos["gas"] = "有害气体浓度：5%"
        indexInfos.append(model2)
        //阳台窗帘
        let model3 = IndexInfoModel.init()
        model3.location = "阳台窗帘"
        model3.curtain = 50
        indexInfos.append(model3)
        //未完待续
        let model4 = IndexInfoModel.init()
        model4.location = "其他位置，暂无设备"
        indexInfos.append(model4)
    }
}
extension IndexView : UITableViewDelegate, UITableViewDataSource {
    //当前列表项的个数
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        4
    }
    
    //设置每一个列表项的高度
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 136
    }
    
    //设置列表项的具体内容
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = IndexInfoCell.init(style: .default, reuseIdentifier: "indexInfoCell")
        cell.selectionStyle = .none
        cell.setInfo(indexInfo: indexInfos[indexPath.row])
        return cell
    }
}
